package com.weilive.core.utils.ini;

import java.util.ArrayList;
import java.util.List;

public class IniData {
	List<SectionData> sectionData = new ArrayList<SectionData>();

	boolean addSection(String sectionname) {
		SectionData section = new SectionData();
		section.setSectionName(sectionname);
		return this.sectionData.add(section);
	}

	boolean addKeyValue(int sectiontitle, String Key, String Value) {
		SectionData sectiondata = (SectionData) this.sectionData
				.get(sectiontitle);
		if (sectiondata == null) {
			return false;
		}

		KeyData data = new KeyData();
		data.setKey(Key);
		data.setValue(Value);

		data.setSection(((SectionData) this.sectionData.get(sectiontitle))
				.getSectionName());
		sectiondata.push(data);
		return true;
	}

	boolean setKeyValue(int sectiontitle, int key, String value) {
		SectionData sectiondata = (SectionData) this.sectionData
				.get(sectiontitle);
		if (sectiondata == null) {
			return false;
		}

		sectiondata.setKeyValue(key, value);
		return true;
	}

	boolean setKeyName(int sectiontitle, int key, String value) {
		SectionData sectiondata = (SectionData) this.sectionData
				.get(sectiontitle);
		if (sectiondata == null) {
			return false;
		}

		sectiondata.setKeyName(sectiontitle, value);
		return true;
	}

	boolean setSectionName(int sectiontitle, String value) {
		SectionData sectiondata = (SectionData) this.sectionData
				.get(sectiontitle);
		if (sectiondata == null) {
			return false;
		}

		sectiondata.setSectionName(value);
		return true;
	}

	void clear() {
		this.sectionData.clear();
	}

	int getCount() {
		return this.sectionData.size();
	}

	String getSectionName(int title) {
		SectionData sectiondata = (SectionData) this.sectionData.get(title);
		if (sectiondata == null) {
			return "";
		}

		return sectiondata.getSectionName();
	}

	int findKey(int section, String key) {
		SectionData sectiondata = (SectionData) this.sectionData.get(section);
		if (sectiondata == null) {
			return -1;
		}

		for (int i = 0; i < sectiondata.getKeyCount(); i++) {
			if (sectiondata.getKeyName(i).equals(key)) {
				return i;
			}
		}

		return -1;
	}

	String getKeyValue(int sectionindex, int keyindex) {
		SectionData section = (SectionData) this.sectionData.get(sectionindex);
		if (section == null) {
			return "";
		}

		return section.getKeyValue(keyindex);
	}

	String getKeyName(int sectionindex, int keyindex) {
		SectionData section = (SectionData) this.sectionData.get(sectionindex);
		if (section == null) {
			return "";
		}

		return section.getKeyName(keyindex);
	}

	int getKeyCount(int title) {
		SectionData section = (SectionData) this.sectionData.get(title);
		if (section == null) {
			return -1;
		}

		return section.getKeyCount();
	}

	boolean delSection(int sectionindex) {
		if (this.sectionData.get(sectionindex) == null) {
			return false;
		}

		this.sectionData.remove(sectionindex);
		return true;
	}

	boolean delKey(int sectionindex, int keyindex) {
		SectionData section = (SectionData) this.sectionData.get(sectionindex);
		if (section == null) {
			return false;
		}

		section.delKey(keyindex);
		return true;
	}
}